﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using RSPJ.Struct;
using RSPJ.Function;
using RSPJ.Windows;
using System.IO;

namespace RSPJ
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        private void menuOpenImageFile_Click(object sender, EventArgs e)
        {
            funcRW data = new funcRW();
            data.open_imagefile();
        }

        private void menuExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void menuEditHeader_Click(object sender, EventArgs e)
        {
            using (SelectFiles sf = new SelectFiles())
            {
                if (sf.ShowDialog() == DialogResult.OK)
                {
                    using (EditHeader eh = new EditHeader(GlobalData.fileList[sf.selectedIndex].Head))
                    {
                        if (eh.ShowDialog() == DialogResult.OK)
                        {
                            GlobalData.fileList[sf.selectedIndex].Head = eh.head;
                        }
                    }
                }
            }
        }

        private void menuAvailableFilesList_Click(object sender, EventArgs e)
        {
            // 打开文件列表
            if (WindowsStatus.availableFilesList == false)
            {
                AvailableFilesList filesWindow = new AvailableFilesList();
                WindowsStatus.availableFilesList = true;
                filesWindow.Show();
            }
        }

        private void menuAvailableBandsList_Click(object sender, EventArgs e)
        {
            // 打开波段列表
            if (WindowsStatus.availableBandsList == false)
            {
                AvailableBandsList bandsWindow = new AvailableBandsList();
                WindowsStatus.availableBandsList = true;
                bandsWindow.Show();
            }
        }

        private void menuCloseAllFiles_Click(object sender, EventArgs e)
        {
            GlobalData.fileList = new List<Files>();
        }

        private void Main_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("你确定要关闭吗！", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                e.Cancel = false;
            else
                e.Cancel = true;
        }

        private void menuRGBtoHSI_Click(object sender, EventArgs e)
        {
            using (SelectBands sb = new SelectBands(3))
            {
                if (sb.ShowDialog() == DialogResult.OK)
                {
                    Files file = new Files();
                    file.Head.Bands = 3;
                    file.Head.Lines = sb.lines;
                    file.Head.Samples = sb.samples;
                    file.Name = "ImageToHSI";
                    file.IntArray = new double[3, file.Head.Lines, file.Head.Samples];
                    for (int i = 0; i < GlobalData.fileList.Count; i++)
                    {
                        if (sb.RFile == GlobalData.fileList[i].Name)
                        {
                            for (int p = 0; p < file.Head.Lines; p++)
                            {
                                for (int q = 0; q < file.Head.Samples; q++)
                                {
                                    file.IntArray[0, p, q] = GlobalData.fileList[i].IntArray[sb.RBand - 1, p, q];
                                }
                            }
                        }
                        if (sb.GFile == GlobalData.fileList[i].Name)
                        {
                            for (int p = 0; p < file.Head.Lines; p++)
                            {
                                for (int q = 0; q < file.Head.Samples; q++)
                                {
                                    file.IntArray[1, p, q] = GlobalData.fileList[i].IntArray[sb.GBand - 1, p, q];
                                }
                            }
                        }
                        if (sb.BFile == GlobalData.fileList[i].Name)
                        {
                            for (int p = 0; p < file.Head.Lines; p++)
                            {
                                for (int q = 0; q < file.Head.Samples; q++)
                                {
                                    file.IntArray[2, p, q] = GlobalData.fileList[i].IntArray[sb.BBand - 1, p, q];
                                }
                            }
                        }
                    }

                    for (int j = 0; j < file.Head.Lines; j++)
                    {
                        for (int k = 0; k < file.Head.Samples; k++)
                        {
                            RGB color = new RGB(Convert.ToByte(file.IntArray[0, j, k]),
                                Convert.ToByte(file.IntArray[1, j, k]), Convert.ToByte(file.IntArray[2, j, k]));
                            HSI outColor = funcColorspace.RGB2HSI(color);
                            file.IntArray[0, j, k] = outColor.Hue;
                            file.IntArray[1, j, k] = outColor.Saturation;
                            file.IntArray[2, j, k] = outColor.Intensity;
                        }
                    }

                    using (ConvertData cd = new ConvertData(file))
                    {
                        if (cd.ShowDialog() == DialogResult.OK)
                        {
                            GlobalData.fileList.Add(cd.file);
                            // 打开波段列表
                            if (WindowsStatus.availableBandsList == false)
                            {
                                AvailableBandsList bandsWindow = new AvailableBandsList();
                                WindowsStatus.availableBandsList = true;
                                bandsWindow.Show();
                            }
                        }
                    }
                }
            }
        }

        private void menuConvertData_Click(object sender, EventArgs e)
        {
            using (SelectFiles sf = new SelectFiles())
            {
                if (sf.ShowDialog() == DialogResult.OK)
                {
                    Files file = GlobalData.fileList[sf.selectedIndex].Clone();
                    using (ConvertData cd = new ConvertData(file))
                    {
                        if (cd.ShowDialog() == DialogResult.OK)
                        {
                            GlobalData.fileList.Add(cd.file);
                            // 打开波段列表
                            if (WindowsStatus.availableBandsList == false)
                            {
                                AvailableBandsList bandsWindow = new AvailableBandsList();
                                WindowsStatus.availableBandsList = true;
                                bandsWindow.Show();
                            }
                        }
                    }
                }
            }
        }

        private void menuMergeData_Click(object sender, EventArgs e)
        {
            using (SelectMultiFiles smf = new SelectMultiFiles())
            {
                if (smf.ShowDialog() == DialogResult.OK)
                {
                    List<Files> selectedfiles = new List<Files>();
                    foreach (int i in smf.selectedIndex)
                    {
                        selectedfiles.Add(GlobalData.fileList[i]);
                    }
                    funcRW rW = new funcRW();
                    Files file;
                    rW.mergedata(selectedfiles, out file);
                    using (ConvertData cd = new ConvertData(file))
                    {
                        if (cd.ShowDialog() == DialogResult.OK)
                        {
                            GlobalData.fileList.Add(cd.file);
                            // 打开波段列表
                            if (WindowsStatus.availableBandsList == false)
                            {
                                AvailableBandsList bandsWindow = new AvailableBandsList();
                                WindowsStatus.availableBandsList = true;
                                bandsWindow.Show();
                            }
                        }
                    }
                }
            }
        }

        private void menuHSItoRGB_Click(object sender, EventArgs e)
        {
            using (SelectBands sb = new SelectBands(3))
            {
                if (sb.ShowDialog() == DialogResult.OK)
                {
                    Files file = new Files();
                    file.Head.Bands = 3;
                    file.Head.Lines = sb.lines;
                    file.Head.Samples = sb.samples;
                    file.Name = "ImageToRGB";
                    file.IntArray = new double[3, file.Head.Lines, file.Head.Samples];
                    for (int i = 0; i < GlobalData.fileList.Count; i++)
                    {
                        if (sb.RFile == GlobalData.fileList[i].Name)
                        {
                            for (int p = 0; p < file.Head.Lines; p++)
                            {
                                for (int q = 0; q < file.Head.Samples; q++)
                                {
                                    file.IntArray[0, p, q] = GlobalData.fileList[i].IntArray[sb.RBand - 1, p, q];
                                }
                            }
                        }
                        if (sb.GFile == GlobalData.fileList[i].Name)
                        {
                            for (int p = 0; p < file.Head.Lines; p++)
                            {
                                for (int q = 0; q < file.Head.Samples; q++)
                                {
                                    file.IntArray[1, p, q] = GlobalData.fileList[i].IntArray[sb.GBand - 1, p, q];
                                }
                            }
                        }
                        if (sb.BFile == GlobalData.fileList[i].Name)
                        {
                            for (int p = 0; p < file.Head.Lines; p++)
                            {
                                for (int q = 0; q < file.Head.Samples; q++)
                                {
                                    file.IntArray[2, p, q] = GlobalData.fileList[i].IntArray[sb.BBand - 1, p, q];
                                }
                            }
                        }
                    }

                    for (int j = 0; j < file.Head.Lines; j++)
                    {
                        for (int k = 0; k < file.Head.Samples; k++)
                        {
                            HSI color = new HSI(file.IntArray[0, j, k],
                                file.IntArray[1, j, k], file.IntArray[2, j, k]);
                            RGB outColor = funcColorspace.HSI2RGB(color);
                            file.IntArray[0, j, k] = outColor.Red;
                            file.IntArray[1, j, k] = outColor.Green;
                            file.IntArray[2, j, k] = outColor.Blue;
                        }
                    }

                    using (ConvertData cd = new ConvertData(file))
                    {
                        if (cd.ShowDialog() == DialogResult.OK)
                        {
                            GlobalData.fileList.Add(cd.file);
                            // 打开波段列表
                            if (WindowsStatus.availableBandsList == false)
                            {
                                AvailableBandsList bandsWindow = new AvailableBandsList();
                                WindowsStatus.availableBandsList = true;
                                bandsWindow.Show();
                            }
                        }
                    }
                }
            }
        }

        private void menuMean_Click(object sender, EventArgs e)
        {
            using (SelectFiles sf = new SelectFiles())
            {
                if (sf.ShowDialog() == DialogResult.OK)
                {
                    funcStatistics fs = new funcStatistics();
                    Files file = fs.Mean(GlobalData.fileList[sf.selectedIndex]);
                    using (ConvertData cd = new ConvertData(file))
                    {
                        if (cd.ShowDialog() == DialogResult.OK)
                        {
                            GlobalData.fileList.Add(cd.file);
                            // 打开波段列表
                            if (WindowsStatus.availableBandsList == false)
                            {
                                AvailableBandsList bandsWindow = new AvailableBandsList();
                                WindowsStatus.availableBandsList = true;
                                bandsWindow.Show();
                            }
                        }
                    }
                }
            }
        }

        private void menuStandardDeviation_Click(object sender, EventArgs e)
        {
            using (SelectFiles sf = new SelectFiles())
            {
                if (sf.ShowDialog() == DialogResult.OK)
                {
                    funcStatistics fs = new funcStatistics();
                    Files file = fs.StandardDeviation(GlobalData.fileList[sf.selectedIndex]);
                    using (ConvertData cd = new ConvertData(file))
                    {
                        if (cd.ShowDialog() == DialogResult.OK)
                        {
                            GlobalData.fileList.Add(cd.file);
                            // 打开波段列表
                            if (WindowsStatus.availableBandsList == false)
                            {
                                AvailableBandsList bandsWindow = new AvailableBandsList();
                                WindowsStatus.availableBandsList = true;
                                bandsWindow.Show();
                            }
                        }
                    }
                }
            }
        }

        private void menuExtreme_Click(object sender, EventArgs e)
        {
            using (SelectFiles sf = new SelectFiles())
            {
                if (sf.ShowDialog() == DialogResult.OK)
                {
                    funcStatistics fs = new funcStatistics();
                    Files file = fs.Extreme(GlobalData.fileList[sf.selectedIndex]);
                    using (ConvertData cd = new ConvertData(file))
                    {
                        if (cd.ShowDialog() == DialogResult.OK)
                        {
                            GlobalData.fileList.Add(cd.file);
                            // 打开波段列表
                            if (WindowsStatus.availableBandsList == false)
                            {
                                AvailableBandsList bandsWindow = new AvailableBandsList();
                                WindowsStatus.availableBandsList = true;
                                bandsWindow.Show();
                            }
                        }
                    }
                }
            }
        }

        private void menuMeanSingle_Click(object sender, EventArgs e)
        {
            using (SelectFiles sf = new SelectFiles())
            {
                if (sf.ShowDialog() == DialogResult.OK)
                {
                    funcStatistics fs = new funcStatistics();
                    double[] mean = fs.MeanSingle(GlobalData.fileList[sf.selectedIndex]);
                    string[,] temp = new string[GlobalData.fileList[sf.selectedIndex].Head.Bands + 1, 2];

                    temp[0, 0] = "bands";
                    temp[0, 1] = "Mean";

                    for (int i = 0; i < GlobalData.fileList[sf.selectedIndex].Head.Bands; i++)
                    {
                        temp[i + 1, 0] = "band" + i.ToString();
                        temp[i + 1, 1] = mean[i].ToString("F2");
                    }

                    string info = "";
                    for (int i = 0; i < GlobalData.fileList[sf.selectedIndex].Head.Bands + 1; i++)
                    {
                        for (int j = 0; j < 2; j++)
                        {
                            info += temp[i, j] + "\t";
                        }
                        info += "\n";
                    }

                    TextPanel cd = new TextPanel(info);
                    cd.Show();
                }
            }
        }

        private void menuAbout_Click(object sender, EventArgs e)
        {
            MessageBox.Show("小组成员：\n  董婧雯  孙梓洋  张劲松\n  陆佳莺  金榕榕  唐以洁\n  江游  夏雨晴  汪倩如\n  温旻  邓浩坤", "关于我们");
        }

        private void menuStandardSingle_Click(object sender, EventArgs e)
        {
            using (SelectFiles sf = new SelectFiles())
            {
                if (sf.ShowDialog() == DialogResult.OK)
                {
                    funcStatistics fs = new funcStatistics();
                    double[] standard = fs.StandardSingle(GlobalData.fileList[sf.selectedIndex]);
                    string[,] temp = new string[GlobalData.fileList[sf.selectedIndex].Head.Bands + 1, 2];

                    temp[0, 0] = "bands";
                    temp[0, 1] = "StandardDeviation";

                    for (int i = 0; i < GlobalData.fileList[sf.selectedIndex].Head.Bands; i++)
                    {
                        temp[i + 1, 0] = "band" + i.ToString();
                        temp[i + 1, 1] = standard[i].ToString("F2");
                    }

                    string info = "";
                    for (int i = 0; i < GlobalData.fileList[sf.selectedIndex].Head.Bands + 1; i++)
                    {
                        for (int j = 0; j < 2; j++)
                        {
                            info += temp[i, j] + "\t";
                        }
                        info += "\n";
                    }

                    TextPanel cd = new TextPanel(info);
                    cd.Show();
                }
            }
        }

        private void menuCovariance_Click(object sender, EventArgs e)
        {
            using (SelectFiles sf = new SelectFiles())
            {
                if (sf.ShowDialog() == DialogResult.OK)
                {
                    funcStatistics funcS = new funcStatistics();
                    double[,] Cov = funcS.Covariance(GlobalData.fileList[sf.selectedIndex]);
                    int bands = GlobalData.fileList[sf.selectedIndex].Head.Bands;
                    string[,] temp = new string[bands + 1, bands + 1];
                    temp[0, 0] = "bands";
                    for (int i = 0; i < bands; i++)
                    {
                        temp[0, i + 1] = "band" + i.ToString();
                    }

                    for (int i = 0; i < bands; i++)
                    {
                        temp[i + 1, 0] = "band" + i.ToString();
                        for (int j = 0; j < bands; j++)
                        {
                            temp[i + 1, j + 1] = Cov[i, j].ToString("F2");
                        }
                    }

                    string info = "";
                    for (int i = 0; i <= bands; i++)
                    {
                        for (int j = 0; j <= bands; j++)
                        {
                            info += temp[i, j] + "\t";
                        }
                        info += "\n";
                    }

                    TextPanel cd = new TextPanel(info);
                    cd.Show();
                }
            }
        }

        private void menuCorrelationCoefficient_Click(object sender, EventArgs e)
        {
            using (SelectFiles sf = new SelectFiles())
            {
                if (sf.ShowDialog() == DialogResult.OK)
                {
                    funcStatistics funcS = new funcStatistics();
                    double[,] Cor = funcS.CorrelationCoefficient(GlobalData.fileList[sf.selectedIndex]);
                    int bands = GlobalData.fileList[sf.selectedIndex].Head.Bands;
                    string[,] temp = new string[bands + 1, bands + 1];
                    temp[0, 0] = "bands";
                    for (int i = 0; i < bands; i++)
                    {
                        temp[0, i + 1] = "band" + i.ToString();
                    }

                    for (int i = 0; i < bands; i++)
                    {
                        temp[i + 1, 0] = "band" + i.ToString();
                        for (int j = 0; j < bands; j++)
                        {
                            temp[i + 1, j + 1] = Cor[i, j].ToString("F2");
                        }
                    }

                    string info = "";
                    for (int i = 0; i <= bands; i++)
                    {
                        for (int j = 0; j <= bands; j++)
                        {
                            info += temp[i, j] + "\t";
                        }
                        info += "\n";
                    }

                    TextPanel cd = new TextPanel(info);
                    cd.Show();
                }
            }
        }

        private void menuSymbioticMatrix_Click(object sender, EventArgs e)
        {
            using (SelectFiles sf = new SelectFiles())
            {
                if (sf.ShowDialog() == DialogResult.OK)
                {
                    funcStatistics funcS = new funcStatistics();
                    int[,] F = funcS.SymbioticMatrix(GlobalData.fileList[sf.selectedIndex]);

                    string info = "";
                    int nummax = 0;
                    for (int u = 0; u <= 255; u++)
                    {
                        for (int v = 0; v <= 255; v++)
                        {
                            info += F[u, v] + "\t";
                            if (F[u, v] > nummax)
                            {
                                nummax = F[u, v];
                            }
                        }
                        info += "\n";
                    }

                    TextPanel cd = new TextPanel(info);
                    cd.Show();

                    for (int p = 0; p < 256; p++)
                    {
                        for (int q = 0; q < 256; q++)
                        {
                            F[p, q] = 255 * F[p, q] / nummax;
                        }
                    }
                    Histogram h = new Histogram(F, "共生矩阵", 3);
                    h.Show();
                }
            }
        }

        private void menuContrast_Click(object sender, EventArgs e)
        {
            using (SelectFiles sf = new SelectFiles())
            {
                if (sf.ShowDialog() == DialogResult.OK)
                {
                    int contrast;
                    funcStatistics funcS = new funcStatistics();
                    contrast = funcS.Contrast(GlobalData.fileList[sf.selectedIndex]);
                    string info = "对比度：" + contrast;
                    TextPanel cd = new TextPanel(info);
                    cd.Show();
                }
            }
        }

        private void menuJointHistogram_Click(object sender, EventArgs e)
        {
            funcStatistics fs = new funcStatistics();
            int[,] b = fs.JointHistogram();
            string info = "";
            int nummax = 0;
            for (int p = 0; p < 256; p++)
            {
                for (int q = 0; q < 256; q++)
                {
                    if (b[p, q] != 0)
                    {
                        info += "x:" + p + ",y:" + q + "，个数：" + b[p, q] + "\n";
                    }
                    if (b[p, q] > nummax)
                    {
                        nummax = b[p, q];
                    }
                }
            }
            TextPanel cd = new TextPanel(info);
            cd.Show();

            for (int p = 0; p < 256; p++)
            {
                for (int q = 0; q < 256; q++)
                {
                    b[p, q] = 255 * b[p, q] / nummax;
                }
            }
            Histogram h = new Histogram(b, "联合直方图", 3);
            h.Show();
        }

        private void menuHHistogram_Click(object sender, EventArgs e)
        {
            funcStatistics fs = new funcStatistics();
            fs.funcHistogram();
        }

        private void menuCumuHistogram_Click(object sender, EventArgs e)
        {
            funcStatistics fs = new funcStatistics();
            fs.CumuHistogram();
        }

        private void menuStretch_Click(object sender, EventArgs e)
        {
            using (SelectFiles sf = new SelectFiles())
            {
                if (sf.ShowDialog() == DialogResult.OK)
                {
                    using (Stretch s = new Stretch())
                    {
                        if (s.ShowDialog() == DialogResult.OK)
                        {
                            funcStretch fs = new funcStretch();
                            Files file = fs.LinearStretch(GlobalData.fileList[sf.selectedIndex], s.left0, s.right0, s.left1, s.right1);
                            using (ConvertData cd = new ConvertData(file))
                            {
                                if (cd.ShowDialog() == DialogResult.OK)
                                {
                                    GlobalData.fileList.Add(cd.file);
                                    // 打开波段列表
                                    if (WindowsStatus.availableBandsList == false)
                                    {
                                        AvailableBandsList bandsWindow = new AvailableBandsList();
                                        WindowsStatus.availableBandsList = true;
                                        bandsWindow.Show();
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        private void menuLinearStretch_Click(object sender, EventArgs e)
        {
            using (SelectFiles sf = new SelectFiles())
            {
                if (sf.ShowDialog() == DialogResult.OK)
                {
                    funcStretch fs = new funcStretch();
                    Files file = fs.twoLinearStretch(GlobalData.fileList[sf.selectedIndex]);
                    using (ConvertData cd = new ConvertData(file))
                    {
                        if (cd.ShowDialog() == DialogResult.OK)
                        {
                            GlobalData.fileList.Add(cd.file);
                            // 打开波段列表
                            if (WindowsStatus.availableBandsList == false)
                            {
                                AvailableBandsList bandsWindow = new AvailableBandsList();
                                WindowsStatus.availableBandsList = true;
                                bandsWindow.Show();
                            }
                        }
                    }
                }
            }
        }

        private void menuEqualization_Click(object sender, EventArgs e)
        {
            using (SelectFiles sf = new SelectFiles())
            {
                if (sf.ShowDialog() == DialogResult.OK)
                {
                    funcStretch fs = new funcStretch();
                    Files file = fs.Equalization(GlobalData.fileList[sf.selectedIndex]);
                    using (ConvertData cd = new ConvertData(file))
                    {
                        if (cd.ShowDialog() == DialogResult.OK)
                        {
                            GlobalData.fileList.Add(cd.file);
                            // 打开波段列表
                            if (WindowsStatus.availableBandsList == false)
                            {
                                AvailableBandsList bandsWindow = new AvailableBandsList();
                                WindowsStatus.availableBandsList = true;
                                bandsWindow.Show();
                            }
                        }
                    }
                }
            }
        }

        private void menuSpecification_Click(object sender, EventArgs e)
        {
            using (SelectFiles sf1 = new SelectFiles())
            {
                if (sf1.ShowDialog() == DialogResult.OK)
                {
                    using (SelectFiles sf2 = new SelectFiles())
                    {
                        if (sf2.ShowDialog() == DialogResult.OK)
                        {
                            funcStretch fs = new funcStretch();
                            Files file = fs.Specification(GlobalData.fileList[sf1.selectedIndex], GlobalData.fileList[sf2.selectedIndex]);
                            using (ConvertData cd = new ConvertData(file))
                            {
                                if (cd.ShowDialog() == DialogResult.OK)
                                {
                                    GlobalData.fileList.Add(cd.file);
                                    // 打开波段列表
                                    if (WindowsStatus.availableBandsList == false)
                                    {
                                        AvailableBandsList bandsWindow = new AvailableBandsList();
                                        WindowsStatus.availableBandsList = true;
                                        bandsWindow.Show();
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        private void menuOIF_Click(object sender, EventArgs e)
        {
            using (SelectBands sb = new SelectBands(3))
            {
                if (sb.ShowDialog() == DialogResult.OK)
                {
                    if (sb.RFile == sb.GFile && sb.GFile == sb.BFile)
                    {
                        for (int i = 0; i < GlobalData.fileList.Count; i++)
                        {
                            if (sb.RFile == GlobalData.fileList[i].Name)
                            {
                                funcStretch fs = new funcStretch();
                                double OIF = fs.CalOIF(GlobalData.fileList[i], sb.RBand - 1, sb.GBand - 1, sb.BBand - 1);
                                string info = "OIF值：" + OIF;
                                TextPanel cd = new TextPanel(info);
                                cd.Show();
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show("请选择同一文件的波段");
                    }
                }
            }
        }

        private void menuBandMath_Click(object sender, EventArgs e)
        {
            using (BandMath bm = new BandMath())
            {
                bm.ShowDialog();
            }
        }

        private void menuKMean_Click(object sender, EventArgs e)
        {
            using (SelectFiles sf = new SelectFiles())
            {
                if (sf.ShowDialog() == DialogResult.OK)
                {
                    using (KMean k = new KMean(GlobalData.fileList[sf.selectedIndex]))
                    {
                        k.ShowDialog();
                    }
                }
            }
        }

        private void menuTasseledCap_Click(object sender, EventArgs e)
        {
            using (SelectFiles sf = new SelectFiles())
            {
                if (sf.ShowDialog() == DialogResult.OK)
                {
                    using (KTFileType k = new KTFileType(GlobalData.fileList[sf.selectedIndex]))
                    {
                        k.ShowDialog();
                    }
                }
            }
        }

        private void menuMaximumLikelihood_Click(object sender, EventArgs e)
        {
            using (SelectFiles sf = new SelectFiles())
            {
                if (sf.ShowDialog() == DialogResult.OK)
                {
                    Files file = GlobalData.fileList[sf.selectedIndex].Clone();
                    Files outFile = new Files();
                    outFile.Name = "MaximumLikelihood(" + file.Name + ")";
                    outFile.Head = file.Head.Clone();
                    outFile.Head.Bands = 3;
                    outFile.IntArray = new double[3, file.Head.Lines, file.Head.Samples];
                    using (OpenFileDialog ofd = new OpenFileDialog())
                    {
                        ofd.Filter = "ROI文件 (*.txt)|*.txt";
                        if (ofd.ShowDialog() == DialogResult.OK)
                        {
                            ROIFile roi = ReadFile.ROI(ofd.FileName);
                            double[,] mean = new double[roi.num, file.Head.Bands];

                            // 计算每个类均值矩阵
                            for (int i = 0; i < roi.num; i++)
                            {
                                for (int j = 0; j < roi.list[i].npts; j++)
                                {
                                    for (int k = 0; k < file.Head.Bands; k++)
                                    {
                                        mean[i, k] += roi.list[i].list[j].b[k];
                                    }
                                }
                                for (int k = 0; k < file.Head.Bands; k++)
                                {
                                    mean[i, k] /= roi.list[i].npts;
                                }
                            }

                            // 计算ROI的协方差矩阵
                            double[][,] Cov = new double[roi.num][,];
                            for (int i = 0; i < roi.num; i++)
                            {
                                Cov[i] = new double[file.Head.Bands, file.Head.Bands];
                                for (int m = 0; m < file.Head.Bands; m++)
                                {
                                    for (int n = 0; n < file.Head.Bands; n++)
                                    {
                                        for (int j = 0; j < roi.list[i].npts; j++)
                                        {
                                            Cov[i][m, n] += (roi.list[i].list[j].b[m] - mean[i, m]) * (roi.list[i].list[j].b[n] - mean[i, n]);
                                        }
                                        Cov[i][m, n] /= roi.list[i].npts;
                                    }
                                }
                            }

                            //协方差逆矩阵
                            double[][,] Inverse = new double[roi.num][,];
                            for (int i = 0; i < roi.num; i++)
                            {
                                Inverse[i] = Matrix.Inverse(Cov[i]);
                            }

                            for (int j = 0; j < file.Head.Lines; j++)
                            {
                                for (int k = 0; k < file.Head.Samples; k++)
                                {
                                    int type = 0;
                                    double tmp = 0;
                                    for (int i = 0; i < roi.num; i++)
                                    {
                                        double[] a = new double[file.Head.Bands];
                                        for (int x1 = 0; x1 < file.Head.Bands; x1++)
                                        {
                                            for (int y1 = 0; y1 < file.Head.Bands; y1++)
                                            {
                                                a[x1] += Inverse[i][x1, y1] * (file.IntArray[y1, j, k] - mean[i, y1]);
                                            }
                                        }

                                        double x3 = 0;
                                        for (int x1 = 0; x1 < file.Head.Bands; x1++)
                                        {
                                            x3 += a[x1] * (file.IntArray[x1, j, k] - mean[i, x1]);
                                        }

                                        //行列式
                                        int[] perm;
                                        int toggle;
                                        double[,] luMatrix = Matrix.MatrixDecompose(Cov[i], out perm, out toggle);
                                        double x4 = Matrix.MatrixDeterminant(Cov[i]);

                                        double g = (-1.0 / 2.0) * Math.Log(x4) - (1.0 / 2.0) * x3;
                                        if (i == 0)
                                        {
                                            tmp = g;
                                            type = i;
                                        }
                                        else if (g > tmp)
                                        {
                                            tmp = g;
                                            type = i;
                                        }
                                    }

                                    outFile.IntArray[0, j, k] = roi.list[type].r;
                                    outFile.IntArray[1, j, k] = roi.list[type].g;
                                    outFile.IntArray[2, j, k] = roi.list[type].b;
                                }
                            }

                            using (ConvertData cd = new ConvertData(outFile))
                            {
                                if (cd.ShowDialog() == DialogResult.OK)
                                {
                                    GlobalData.fileList.Add(cd.file);
                                    // 打开波段列表
                                    if (WindowsStatus.availableBandsList == false)
                                    {
                                        AvailableBandsList bandsWindow = new AvailableBandsList();
                                        WindowsStatus.availableBandsList = true;
                                        bandsWindow.Show();
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
